package com.system.controller;

import com.system.pojo.DepartmentSalary;
import com.system.pojo.EmployeeSalary;
import com.system.service.DepartmentSalaryService;
import com.system.service.EmployeeSalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;

/**
 * Created by 陈文杰 on 2022/6/10 21:45
 */
@Controller
public class DepartmentSalaryController {

    @Autowired
    DepartmentSalaryService departmentSalaryService;

    @Autowired
    EmployeeSalaryService employeeSalaryService;

    @RequestMapping("/toDepartmentSalary")
    public ModelAndView toDepartmentSalary() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("departmentSalaries",departmentSalaryService.queryDepartmentSalaryAll());
        modelAndView.setViewName("departmentSalary");
        return modelAndView;
    }

    @RequestMapping("/toChangeSalary")
    public ModelAndView toChangeSalary(String deptName){
        ModelAndView modelAndView = new ModelAndView();
        DepartmentSalary departmentSalary = departmentSalaryService.queryDepartmentSalaryByDeptName(deptName);
        modelAndView.addObject("departmentSalary",departmentSalary);
        modelAndView.setViewName("changeDepartmentSalary");
        return modelAndView;
    }

    @RequestMapping("/changeDepartmentSalary")
    public String changeDepartmentSalary(
            int deptSalId,
            double deptBasePay,
            double deptAttSal,
            double deptInsuranceSal,
            double deptTechnologySal,
            double deptManageSal,
            double deptCommonSal,
            double deptPracticeSal
    ){
        System.out.println(deptBasePay);
        ModelAndView modelAndView = new ModelAndView();
        HashMap<String, Object> map = new HashMap<>();
        DepartmentSalary departmentSalary = departmentSalaryService.queryDepartmentSalaryByDeptSalId(deptSalId);
        String deptName = departmentSalary.getDeptName();
        System.out.println(deptName);

//        List<EmployeeSalary> employeeSalaries = employeeSalaryService.queryEmployeeSalaryByDeptName(departmentSalary.getDeptName());
        if (!departmentSalary.getDeptBasePay().equals(deptBasePay)){

            //修改部门
            map.put("deptSalId",deptSalId);
            map.put("deptBasePay",deptBasePay);

            departmentSalaryService.updateDepartmentSalaryByDeptSalId(map);
            //修改员工
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("empBasePay",deptBasePay);
            map1.put("deptName",deptName);
            double empPracticalSal1=deptBasePay+deptAttSal-deptInsuranceSal;
            System.out.println("deptBasePay是:"+deptBasePay+"deptAttSal是:"+deptAttSal+"deptInsuranceSal是:"+deptInsuranceSal);
            System.out.println("实发工资是:"+empPracticalSal1);
            map1.put("empPracticalSal",empPracticalSal1);
//            employeeSalaryService.updateEmployeeSalaryByEmpSalId(map);
            employeeSalaryService.updateEmployeeSalaryByDeptName(map1);
            //同步更改员工工资信息
//            for (EmployeeSalary employeeSalary : employeeSalaries) {
//               employeeSalary.setEmpBasePay(deptBasePay);
//                System.out.println(employeeSalary.getEmpName());
//                employeeSalary.setEmpPracticalSal(employeeSalary.getEmpBasePay()+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());
//                HashMap<String,Object > map1 = new HashMap<>();
//                employeeSalary.setEmpAttSal(deptAttSal);
//                map1.put("empBasePay",deptBasePay);
//                map1.put("empPracticalSal",deptBasePay+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());
//                map1.put("empName",employeeSalary.getEmpName());
//                employeeSalaryService.updateEmployeeSalaryByEmpName(map1);
//            }
        }else {
            return "redirect:toDepartmentSalary";
        }
//        if (departmentSalary.getDeptAttSal()!=deptAttSal) {map.put("deptAttSal",deptAttSal);
//            for (EmployeeSalary employeeSalary : employeeSalaries) {
//                HashMap<String,Object > map1 = new HashMap<>();
//                employeeSalary.setEmpAttSal(deptAttSal);
//                map1.put("empAttSal",deptAttSal);
//                map1.put("empPracticalSal",employeeSalary.getEmpBasePay()+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());
//                employeeSalaryService.updateEmployeeSalaryByEmpName(map1);
//            }
//        }
//        if (departmentSalary.getDeptInsuranceSal()!=deptInsuranceSal) {map.put("deptInsuranceSal",deptInsuranceSal);
//            for (EmployeeSalary employeeSalary : employeeSalaries) {
//
//                employeeSalary.setEmpPracticalSal(employeeSalary.getEmpBasePay()+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());
//                HashMap<String,Object > map1 = new HashMap<>();
//                employeeSalary.setEmpInsurance(deptInsuranceSal);
//                map1.put("empInsurance",deptInsuranceSal);
//                map1.put("empPracticalSal",employeeSalary.getEmpBasePay()+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());
//                employeeSalaryService.updateEmployeeSalaryByEmpName(map1);
//            }
//        }
//        if (departmentSalary.getDeptTechnologySal()!=deptTechnologySal) map.put("deptTechnologySal",deptTechnologySal);
//        if (departmentSalary.getDeptManageSal()!=deptManageSal) map.put("deptManageSal",deptManageSal);
//        if (departmentSalary.getDeptCommonSal()!=deptCommonSal) map.put("deptCommonSal",deptCommonSal);
//        if (departmentSalary.getDeptPracticeSal()!=deptPracticeSal) map.put("deptPracticeSal",deptPracticeSal);
//        departmentSalaryService.updateDepartmentSalaryByDeptSalId(map);
        //                employeeSalary.setEmpPracticalSal(employeeSalary.getEmpBasePay()+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());



        return "redirect:toDepartmentSalary";
    }


    /*@RequestMapping("/changeDepartmentSalary")
    public String changeDepartmentSalary(
            int deptSalId,
            double deptBasePay,
            double deptAttSal,
            double deptInsuranceSal,
            double deptTechnologySal,
            double deptManageSal,
            double deptCommonSal,
            double deptPracticeSal
            ){
        ModelAndView modelAndView = new ModelAndView();
        HashMap<String, Object> map = new HashMap<>();
        DepartmentSalary departmentSalary = departmentSalaryService.queryDepartmentSalaryByDeptSalId(deptSalId);
        String deptName = departmentSalary.getDeptName();
        map.put("deptSalId",deptSalId);
        List<EmployeeSalary> employeeSalaries = employeeSalaryService.queryEmployeeSalaryByDeptName(departmentSalary.getDeptName());
        if (!departmentSalary.getDeptBasePay().equals(deptBasePay)){ map.put("deptBasePay",deptBasePay);
            //同步更改员工工资信息
//            for (EmployeeSalary employeeSalary : employeeSalaries) {
//               employeeSalary.setEmpBasePay(deptBasePay);
//                System.out.println(employeeSalary.getEmpName());
//                employeeSalary.setEmpPracticalSal(employeeSalary.getEmpBasePay()+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());
//                HashMap<String,Object > map1 = new HashMap<>();
//                employeeSalary.setEmpAttSal(deptAttSal);
//                map1.put("empBasePay",deptBasePay);
//                map1.put("empPracticalSal",deptBasePay+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());
//                map1.put("empName",employeeSalary.getEmpName());
//                employeeSalaryService.updateEmployeeSalaryByEmpName(map1);
//            }
        }
        if (departmentSalary.getDeptAttSal()!=deptAttSal) {map.put("deptAttSal",deptAttSal);
            for (EmployeeSalary employeeSalary : employeeSalaries) {
                HashMap<String,Object > map1 = new HashMap<>();
                employeeSalary.setEmpAttSal(deptAttSal);
                map1.put("empAttSal",deptAttSal);
                map1.put("empPracticalSal",employeeSalary.getEmpBasePay()+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());
                employeeSalaryService.updateEmployeeSalaryByEmpName(map1);
            }
        }
        if (departmentSalary.getDeptInsuranceSal()!=deptInsuranceSal) {map.put("deptInsuranceSal",deptInsuranceSal);
            for (EmployeeSalary employeeSalary : employeeSalaries) {

                employeeSalary.setEmpPracticalSal(employeeSalary.getEmpBasePay()+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());
                HashMap<String,Object > map1 = new HashMap<>();
                employeeSalary.setEmpInsurance(deptInsuranceSal);
                map1.put("empInsurance",deptInsuranceSal);
                map1.put("empPracticalSal",employeeSalary.getEmpBasePay()+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());
                employeeSalaryService.updateEmployeeSalaryByEmpName(map1);
            }
        }
        if (departmentSalary.getDeptTechnologySal()!=deptTechnologySal) map.put("deptTechnologySal",deptTechnologySal);
        if (departmentSalary.getDeptManageSal()!=deptManageSal) map.put("deptManageSal",deptManageSal);
        if (departmentSalary.getDeptCommonSal()!=deptCommonSal) map.put("deptCommonSal",deptCommonSal);
        if (departmentSalary.getDeptPracticeSal()!=deptPracticeSal) map.put("deptPracticeSal",deptPracticeSal);
        departmentSalaryService.updateDepartmentSalaryByDeptSalId(map);
        //                employeeSalary.setEmpPracticalSal(employeeSalary.getEmpBasePay()+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());



        return "redirect:toDepartmentSalary";
    }*/

}
